package com.controller;

import com.entity.EFence;
import com.repository.EFenceRepository;
import com.util.StringUtil;
import javafx.scene.effect.Light;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Point;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *特力惠空间坐标转换
 * </p>
 *
 * @author zhangqing
 * @since 2021/2/24
 */
@RestController
@RequestMapping("/space")
public class TlwSpaceController {
    @Autowired
    private EFenceRepository eFenceRepository;
    @GetMapping("efence")
    public String spaceParse(){
        List<EFence> eFences = eFenceRepository.selectList(null);
        for (EFence eFence : eFences) {
            String point =parsePoint( eFence.getPolygonCircleCenter());

            String polygon =parsePolygon( eFence.getBoundaryPoint(),false);
            eFence.setBoundaryPoint(polygon);
            eFence.setPolygonCircleCenter(point);
            eFenceRepository.updateById(eFence);
        }
        return null;
    }
    public  String parsePolygon(String polygon,boolean tobig){
        if(StringUtils.isEmpty(polygon)){
            return null;
        }
        String[] split = polygon.split(",");
        StringBuilder builder = new StringBuilder();
        if(!tobig) {
            builder.append("(");
        }
        for (int i = 0; i < split.length; i++) {
            if(i !=0 ){
                builder.append(",");
            }

            String[] split2 = split[i].split(" ");
            if(split2.length==2) {
                builder.append(String.format("(%s,%s)", split2[0], split2[1]));
            }
            if(split2.length==3){
                builder.append(String.format("(%s,%s)", split2[1], split2[2]));
            }

        }
        if(!tobig) {
            builder.append(")");
        }

        return builder.toString();
    }
    public String parsePoint(String point){
        if(StringUtils.isEmpty(point)){
            return null;
        }
        String[] s = point.split(" ");
        StringBuilder builder = new StringBuilder();
        builder.append(String.format("(%s,%s)",s[0],s[1]));
        return builder.toString();

    }

}
